package com.zhaoxi.demo.demo1;

import com.zhaoxi.demo.demo1.config.BeanConfig;
import com.zhaoxi.demo.demo1.config.BeanConfig1;
import com.zhaoxi.demo.demo1.dao.SomeDAOImpl;
import com.zhaoxi.demo.demo1.service.SomeService;
import com.zhaoxi.demo.demo1.service.SomeServiceImpl;
import com.zhaoxi.demo.page.PageBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author zxiaoming
 * @Description: 测试基于注解方式的容器对bean进行管理
 * @date 2021/2/2523:42
 */
public class TestAnnotationContext {
    public static void main(String[] args) {
        //AnnotationConfigApplicationContext注解配置的容器类
        //method1();
    //    method2();
        method3();
        //method4();
    }

    private static void method1() {
        ApplicationContext context = new AnnotationConfigApplicationContext("com.zhaoxi.demo.demo1");
        SomeService someService = context.getBean("someService", SomeService.class);
        someService.doSomething();
    }
    private static void method2() {
        ApplicationContext context = new AnnotationConfigApplicationContext(SomeServiceImpl.class, SomeDAOImpl.class, PageBean.class);
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }
        SomeService someService = context.getBean("someServiceImpl", SomeService.class);
        someService.doSomething();
    }
    //基于配置类里面方法上面的@Bean注解完成
    private static void method3() {
        ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
        //手动装配的实例bean名称就是方法名称
        SomeService someService = context.getBean("someService1", SomeService.class);
        someService.doSomething();
    }
    //基于配置类上的import注解来完成
    private static void method4() {
        ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig1.class);
        //生成bean的名称就是装配类的全名称
        SomeService someService = context.getBean("com.zhaoxi.demo.demo1.service.SomeServiceImpl", SomeService.class);
        someService.doSomething();
    }
}
